import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author: gao
 * @create-date: 2025/4/22 8:08
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public int sumNumbers(TreeNode root) {
        return dfs(root, 0);
    }

    private int dfs(TreeNode root, int preSum) {
        preSum = preSum * 10 + root.val;

        if (root.left == null && root.right == null) {
            return preSum;
        }

        int ret = 0;
        if (root.left != null) ret += dfs(root.left, preSum);
        if (root.right != null) ret += dfs(root.right, preSum);

        return ret;
    }

    public static TreeNode BuildTree(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);

        int i = 1;
        while (!queue.isEmpty() && i < nums.length) {
            TreeNode currentNode = queue.poll();

            if (i < nums.length && nums[i] != -1) {
                currentNode.left = new TreeNode(nums[i]);
                queue.offer(currentNode.left);
            }
            i++;

            if (i < nums.length && nums[i] != -1) {
                currentNode.right = new TreeNode(nums[i]);
                queue.offer(currentNode.right);
            }
            i++;
        }

        return root;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int len = in.nextInt();
            int[] nums = new int[len];
            for (int i = 0; i < len; i++) {
                nums[i] = in.nextInt();
            }
            TreeNode root = BuildTree(nums);
            Solution solution = new Solution();
            System.out.println(solution.sumNumbers(root));
        }
    }
}